#!/usr/bin/env node
// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// =============================================================================

const path = require('path');
const spawn = require('cross-spawn');
const opn = require('opn');
const shell = require('shelljs');
var argv = require('minimist')(process.argv.slice(2));

/** List of vue demos. */
const VUE_DEMOS = new Set(['game_of_life', 'vue-demo', 'latent-space-explorer']);

const demoPath = argv._[0];

const demoFolder = path.basename(demoPath);
const outputPath = path.join(demoPath, 'bundle.js')
const tsPath = path.join(demoPath, demoFolder + '.ts');

if (VUE_DEMOS.has(demoFolder)) {
  shell.cd('demos');
  const cmd = `./node_modules/.bin/poi ${demoFolder} -o`;
  if (shell.exec(cmd).code !== 0) {
    shell.echo(`Failed building ${demoFolder}`);
    shell.exit(1);
  }
  return;
}

const cmd = path.join('demos', 'node_modules', '.bin', 'watchify');

const tsifyCmd = ['tsify', '-p', 'demos/tsconfig-dev.json'];

// tsifyCmd is passed to the watchify command, so the arguments should be
// wrapped in [].
tsifyCmd[0] = '[' + tsifyCmd[0];
tsifyCmd[tsifyCmd.length - 1] += ']';

const watchify = spawn(cmd, [tsPath, '-p', ...tsifyCmd, '-v',
    '--debug', '-o' , outputPath], {detached: false});
watchify.stdout.pipe(process.stdout);
watchify.stderr.pipe(process.stderr);

let httpServerStarted = false;

console.log('Waiting for initial compile...');
watchify.stderr.on('data', (data) => {
  if (data.toString().includes(`bytes written to`)) {
    if (!httpServerStarted) {
      const httpCmd = path.join('node_modules', '.bin', 'http-server');
      const httpServer = spawn(httpCmd, ['-c-1'], { detached: false});

      httpServer.stdout.on('data', data => {
        data = data.toString().split('\n')[0].trim()
        if (data.startsWith('http://127.0.0.1:')) {
          // Open a browser pointing to the demo.
          opn(path.join(data, demoPath));
        }
      });

      httpServer.stdout.pipe(process.stdout);
      httpServer.stderr.pipe(process.stderr);
      httpServerStarted = true;
    }
  }
});
